﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Blazing.BusinessObjects.Tagging;
using System.Collections.Generic;
using System.Web.Script.Services;
using Blazing.WebFacade;

namespace Blazing.UI.Cms.Admin.Usercontrols
{
    public partial class SelectTags : System.Web.UI.UserControl
    {
        private bool _allowAdd = false;
        public bool AllowAdd
        {
            get { return _allowAdd; }
            set { _allowAdd = value;
                  PanelParent.Visible = AllowAdd;
            }
        }

        public Taxonomy SelectedTaxonomy
        {
            get
            {
                return BindableDropDownListExtender1.BoundValue as Taxonomy;
            }
            set
            {
                BindableDropDownListExtender1.BoundValue = value;
            }
        }

        public TaxonomyTag SelectedParentTag
        {
            get
            {
                if (CheckBox1.Checked)
                    return BindableDropDownListExtender2.BoundValue as TaxonomyTag;
                else
                    return null;
            }
            set
            {
                BindableDropDownListExtender2.BoundValue = value;
            }
        }

        public List<string> Tags
        {
            get
            {
                return new List<string>(txtTags.Text.Split(',',';',':','/'));
            }
            set
            {
                txtTags.Text = ToStringFromArray(value);
            }
        }

        public void Clear()
        {
            txtTags.Text = "";
        }

        private string ToStringFromArray(IList<string> list)
        {
            string retval = "";
            foreach (string item in list)
            {
                retval += (retval.Length > 0 ? "," : "") + item;
            }
            return retval;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                PanelParent.Visible = AllowAdd;
                CheckBox1.Checked = !AllowAdd;
            }
        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            ddlParentTag.Enabled = CheckBox1.Checked;
        }        
    }
}